<?php
declare(strict_types=1);

namespace App\Feedback;

use App\Controller;
use Field\StoreProductConst;

// hook controller_feedback_index_use.php

/**
 * @title   需求反馈
 * @auth    true
 * @login   false
 * @menu    false
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    10
 */
class Index extends Controller
{
    // hook controller_feedback_index_public_start.php

    // hook controller_feedback_index_public_end.php
    public int $CateID=37;
    /**
     * @title    首页
     * @rank    99
     */
    public function action_Index()
    {
        // hook controller_feedback_index_index_start.php
        $ListProduct = $this->StoreProduct->select([], [StoreProductConst::Rank => 1], 1, 100, 'Title,ProductID');
        
        return $this->View(get_defined_vars());
        // hook controller_feedback_index_index_end.php
    }

    /**
     * @title   详情页
     * @rank    99
     */
    public function action_Detail()
    {
        // hook controller_feedback_index_detail_start.php

        return $this->View(get_defined_vars());
        // hook controller_feedback_index_detail_end.php
    }


    /**
     * @title    需求反馈提交
     * @rank    99
     */
    public function action_Add()
    {
        // hook controller_feedback_index_add_start.php

        $title = $this->request->param('title', '');
        $cate = $this->CateID;
        $contact = $this->request->param('contact', '');
        $product = $this->request->param('product', '');
        $shopNick = $this->request->param('shop_nick', '');
        $product = mb_substr($product, 0, 100);
        $content = $this->request->param('content', '');

        if (!in_array($cate, [35, 36, 37, 38])) {
            $this->response('0001', [], '参数错误');
        }
        $isImg = 0;
        $content = "客户联系方式:" . $contact . "/n" . $content;
        $content = "授权店铺名称:" . $shopNick . "/n" . $content;
        
        if (strstr($content, '<img')) {
            $isImg = 1;
        }
        $title = mb_substr($title, 0, 63);
        $articleID = $this->SysArticle->insert([
            'Title' => $title,
            'CateID' => $cate,
            'Tags' => $product,
            'IsImage' => $isImg
        ]);
        $this->SysArticleContent->insert([
            'ArticleID' => $articleID,
            'Content' => $content
        ]);
        
        $this->response('0000');
        // hook controller_feedback_index_add_end.php
    }

    /**
     * @title    工单
     * @rank    99
     */
    public function action_Case(){
        $ListProduct = $this->StoreProduct->select([], [StoreProductConst::Rank => 1], 1, 100, 'Title,ProductID');
        return $this->View(get_defined_vars());
    }


    /**
     * @title    提交工单
     * @rank    99
     */
    public function action_CaseAdd(){
        // hook controller_feedback_index_caseadd_start.php
        $this->CateID=38;
         $this->action_Add();
        // hook controller_feedback_index_caseadd_end.php
    }


    /**
     * @title    发票申请
     * @rank    99
     */
    public function action_Invoice(){
        // hook controller_feedback_index_invoice_start.php
        return $this->View(get_defined_vars());
        // hook controller_feedback_index_invoice_end.php
    }


    /**
     * @title    提交发票申请
     * @rank    99
     */
    public function action_InvoiceAdd(){
        $this->CateID=39;
        //独立写逻辑吧 直接写 json
        $code = $this->request->param('code', '');//同一信用代码

        $shopNick = $this->request->param('shop_nick', '');//授权店铺昵称

        $email = $this->request->param('email', '');//接收邮箱

        $phone = $this->request->param('phone', '');//接收手机

        $title = $this->request->param('title', '');//发票抬头

        $money = (int)$this->request->param('money', '');//发票抬头

        $remark = $this->request->param('remark', '');//备注
        
        
        //依次对上面的字段做校验
        if(empty($phone) || empty($code) || empty($shopNick) || empty($email) || empty($title) || empty($money)){
            $this->response('0001', [], '参数错误123');
        }
        //校验格式是否正确
        if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            $this->response('0001', [], '联系方式格式错误');
        }
        if(!preg_match("/^[0-9a-zA-Z]+$/",$code)){
            $this->response('0001', [], '信用代码格式错误');
        }

        if(!preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/",$email)){
            $this->response('0001', [], '邮箱格式错误');
        }

        //写入 json
        $articleID = $this->SysArticle->insert([
            'Title' => $title,
            'CateID' => $this->CateID,
            'Tags' => $shopNick,
            'IsImage' => 0
        ]);
        $this->SysArticleContent->insert([
            'ArticleID' => $articleID,
            'Content' => xn_json_encode([
                'phone' => $phone,
                'code' => $code,
                'shopNick' => $shopNick,
                'email' => $email,
                'title' => $title,
                'money' => $money,
                'remark'=>$remark
            ])
        ]);
        
        $this->response('0000');
    }


    // hook controller_feedback_index_end.php
}

?>